package cn.cdeden.resource.api.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.resource.api.domain
 * @ClassName : RemoteFilesDownload
 * @Author : Yu.
 * @Description : // TODO
 * @Date : 2024/12/5 17:50
 * @Version : 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RemoteFilesDownload implements Serializable {

    private static final long serialVersionUID = 1L;


    /**
     * 数据id
     */
    private String id;

    /**
     * 文件的存储id
     **/
    private String storageId;


    /**
     * 文件的版本
     **/
    private String versionId;

    /**
     * 文件的版本名称
     */
    private String versionName;

    /**
     * 编号
     **/
    private String dateId;

    /**
     * 父亲的id
     **/
    private String parentId;

    /**
     * 设置文件的大小
     */
    private Long fileSize;

    /**
     * 文件名称 或文件夹名称
     **/
    private String fileName;

    public String getFileNameSuffix() {
        if (this.isFolder){
            return this.fileName;
        }else {
            return this.fileName +"."+this.fileSuffix;
        }
    }

    /**
     *  文件的后缀
     **/
    private String fileSuffix;

    /**
     *  判断是否是文件夹  false 文件   true 文件夹
     **/
    private boolean isFolder = false;

    /**
     *  设置库的类型
     **/
    private String libraryType ;


    private String key;

    private String bucketName;

    private String service;

    /**
     * 下载文件地址
     **/
    private String downloadPath;


    /**
     * 儿子数据
     **/
    private List<RemoteFilesDownload> children = new ArrayList<>();



    /**
     * @author Yu.
     * @Description // list 转 树结构  确定头节点
     * @Date 2023/11/22 15:09
     * @param filesTrees
     * @param ids
     * @param libraryType
     * @return java.util.List<com.cdyd.ecm.domain.TreeDownloadFiles>
     **/
    public static  List<RemoteFilesDownload> listToTerr(List<RemoteFilesDownload> filesTrees ,List<String> ids,String libraryType){
        if ( filesTrees == null || filesTrees.size() < 1 || ids == null || ids.size() < 1 ){
            return  new ArrayList<>();
        }

        filesTrees.forEach(tree -> tree.setLibraryType(libraryType) ); //设置库的类型
        Map<String, List<RemoteFilesDownload>> collect = filesTrees.stream().collect(Collectors.groupingBy(RemoteFilesDownload::getParentId));
        for (RemoteFilesDownload filesTree : filesTrees) {
            String of = String.valueOf(filesTree.getDateId());
            List<RemoteFilesDownload> files = collect.get(of);
            filesTree.setChildren(files);
        }

        return filesTrees.stream().filter(downloadFiles -> ids.contains(downloadFiles.getParentId().toString())).collect(Collectors.toList());
    }



    /**
     * @author Yu.
     * @Description // list 转 树结构
     * @Date 2023/11/22 15:09
     * @param filesTrees  需要转的数据
     * @param libraryType
     * @return java.util.List<com.cdyd.ecm.domain.TreeDownloadFiles>
     **/
    public static  List<RemoteFilesDownload> listToTerr(List<RemoteFilesDownload> filesTrees ,String libraryType){
        if ( filesTrees == null || filesTrees.size() < 1 ){
            return  new ArrayList<>();
        }

        filesTrees.forEach(tree -> {
            tree.setLibraryType(libraryType);
            String treeParentId = tree.getParentId();
            if ( treeParentId == null){
                tree.setParentId("0");
            }
        } ); //设置库的类型
        Map<String, List<RemoteFilesDownload>> collect = filesTrees.stream().collect(Collectors.groupingBy(RemoteFilesDownload::getParentId));
        for (RemoteFilesDownload filesTree : filesTrees) {
            String of = String.valueOf(filesTree.getDateId());
            List<RemoteFilesDownload> files = collect.get(of);
            filesTree.setChildren(files);
        }
        List<RemoteFilesDownload> downloadList = filesTrees.stream()
            .filter(downloadFiles ->
                downloadFiles.getParentId().equals("0")
            )
            .collect(Collectors.toList());
        return downloadList;
    }










}
